package com.ifreshwing.activity.base;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.ifreshwing.ParkSysApplication;
import com.ifreshwing.adapter.ParkingListAllAdapter;
import com.ifreshwing.userpark.R;
import com.ifreshwing.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lz on 2017/5/2.
 */

public class ParkListActivity extends BaseActivity {
    @BindView(R.id.titleBar_back)
    ImageView titleBarBack;
    @BindView(R.id.titleBar_text)
    TextView titleBarText;
    ParkingListAllAdapter adapter;
    @BindView(R.id.park_lst)
    ListView parkLst;
    @BindView(R.id.swipe_refresh)
    SwipeRefreshLayout swipeRefresh;
    @BindView(R.id.coor)
    CoordinatorLayout coor;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_parklst);
        ButterKnife.bind(this);
        String RegionName = getIntent().getExtras().getString("RegionName");
        titleBarText.setText(RegionName);
        titleBarBack.setVisibility(View.VISIBLE);
        adapter = new ParkingListAllAdapter(ParkSysApplication.getInstance().getList(), this);
        adapter.notifyDataSetChanged();
        parkLst.setAdapter(adapter);
        swipeRefresh.setOnRefreshListener(() -> new Handler().postDelayed(() -> {
            swipeRefresh.setRefreshing(false);
            handler.sendEmptyMessage(0);
        }, 1000));
        parkLst.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                swipeRefresh.setEnabled(firstVisibleItem == 0);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            ToastUtil.showSnackBar(coor, "执行了刷新事件");

        }
    };

    @OnClick(R.id.titleBar_back)
    public void onViewClicked() {
        finish();
    }
}
